package com.zhys.manage.modular.business.controller;

import com.zhys.manage.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.zhys.manage.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.zhys.manage.common.persistence.model.Drug;
import com.zhys.manage.modular.business.service.IDrugService;

/**
 * 药品管理控制器
 *
 * @author fengshuonan
 * @Date 2018-01-17 16:57:05
 */
@Controller
@RequestMapping("/drug")
public class DrugController extends BaseController {

    private String PREFIX = "/business/drug/";

    @Autowired
    private DrugService drugService;

    /**
     * 跳转到药品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "drug.html";
    }

    /**
     * 跳转到添加药品管理
     */
    @RequestMapping("/drug_add")
    public String drugAdd() {
        return PREFIX + "drug_add.html";
    }

    /**
     * 跳转到修改药品管理
     */
    @RequestMapping("/drug_update/{drugId}")
    public String drugUpdate(@PathVariable Integer drugId, Model model) {
        Drug drug = drugService.selectById(drugId);
        model.addAttribute("item",drug);
        LogObjectHolder.me().set(drug);
        return PREFIX + "drug_edit.html";
    }

    /**
     * 获取药品管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return drugService.selectList(null);
    }

    /**
     * 新增药品管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Drug drug) {
        drugService.insert(drug);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除药品管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer drugId) {
        drugService.deleteById(drugId);
        return SUCCESS_TIP;
    }

    /**
     * 修改药品管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Drug drug) {
        drugService.updateById(drug);
        return super.SUCCESS_TIP;
    }

    /**
     * 药品管理详情
     */
    @RequestMapping(value = "/detail/{drugId}")
    @ResponseBody
    public Object detail(@PathVariable("drugId") Integer drugId) {
        return drugService.selectById(drugId);
    }
}
